from playwright.sync_api import Page


class GenshinPage:
    """
    原神页面
    """
    # 搜索-输入框
    _kw = "#kw"
    # 搜索-按钮
    _su = "#su"
    # 标题-原神
    _genshin_title = "//a[contains(text(),'原神')]"
    # 原神-进入官网-按钮
    _officialWebsite = "#pz_e8KxQhR1VIpz-beeErv9fjJ>.pz-button"

    """
    页面方法层
    """

    def __init__(self, page: Page):
        self.page = page

    def baidu(self):
        """
        登录
        """
        self.page.goto('https://www.baidu.com')
        self.page.wait_for_load_state()
        return self

    def search(self, search):
        """
        搜索
        """
        self.page.locator(self._kw).fill(search)
        self.page.locator(self._su).click()
        return self

    def genshin(self):
        """
        原神-进入官网
        """
        print(self.page.title())
        with self.page.expect_popup() as genshin_info:
            # 点击原神标题，打开进入官网页面
            self.page.locator(self._genshin_title).click()
            # 获取进入官网页面的page
            genshinPage = genshin_info.value
            # 等待进入官网页面加载
            genshinPage.wait_for_load_state()
            print(genshinPage.title())
        with genshinPage.expect_popup() as genshin_new_info:
            # 点击进入官网，打开原神官网页面
            genshinPage.locator(self._officialWebsite).click()
            # 获取官网页面的page
            genshinNewPage = genshin_new_info.value
            # 等待官网页面加载
            genshinNewPage.wait_for_load_state()
            print(genshinNewPage.title())
        # 查看官网页面
        genshinNewPage.wait_for_timeout(5000)
        # 关闭官网页面
        genshinNewPage.close()
        # 查看进入官网页面
        genshinPage.wait_for_timeout(5000)
        # 关闭进入官网页面
        genshinPage.close()
        # 等待百度页面关闭
        self.page.wait_for_timeout(5000)
